//
//  LJFCustomCollectionCell.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/17.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFCustomCollectionCell.h"
#import "UIImageView+AFNetworking.h"
#import "LJFWedController.h"


@implementation LJFCustomCollectionCell


- (void)awakeFromNib
{
    [_myTableView registerNib:[UINib nibWithNibName:@"LJFSmallCell" bundle:nil] forCellReuseIdentifier:@"LJFSmallCell"];
}


-(void)setCollectionRow:(NSUInteger)CollectionRow
{
    _CollectionRow = CollectionRow;
    
    if (CollectionRow != 2)
    {
        [self createHeadView];
    }
    else
    {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0)];
         _myTableView.tableHeaderView = view;
    }
    
    [_myTableView reloadData];
}

- (void)createHeadView;
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
    
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"进行中",@"已结束"]];
    segment.frame = CGRectMake((self.frame.size.width-200)/2.0,10 , 200, 30);
    segment.selectedSegmentIndex = _index;
    segment.tintColor = [UIColor colorWithRed:24/255.0 green:130/255.0 blue:31/255.0 alpha:1.0];
    [segment addTarget:self action:@selector(changeData:) forControlEvents:UIControlEventValueChanged];
    
    [view addSubview:segment];
    
    _myTableView.tableHeaderView = view;
}

- (void)changeData:(UISegmentedControl *)segment
{

    _index = segment.selectedSegmentIndex;
    
    [_myTableView reloadData];
}



#pragma mark -UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_CollectionRow == 0)
    {
        if (_giftArr.count != 0)
        {
            NSArray *arr = _giftArr[_index];
            return arr.count;
        }
        else
        {
            return 0;
        }
    }
    else if(_CollectionRow == 1)
    {
        if (_activityArr.count != 0)
        {
            NSArray *arr = _activityArr[_index];
            return arr.count;
        }
        else
        {
            return 0;
        }

    }
    else
    {
        return _partArr.count;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_CollectionRow == 2)
    {
        NSDictionary *dict = _partArr[indexPath.row];
        
        LJFSmallCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LJFSmallCell"];
        
        [cell.imgView setImageWithURL:[NSURL URLWithString:dict[@"thumb"]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        cell.goodsName.text = dict[@"goods_name"];
        cell.money.text = [NSString stringWithFormat:@"%@",dict[@"shop_price"]];
        
        return cell;
    }
    else
    {
        LJFSmallCustomCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LJFSmallCustomCell"];
        
        cell.mydelegate = self;
        
        cell.button.tag = 310 + indexPath.row;
        
        if (_CollectionRow == 0)
        {
            NSDictionary *dict = _giftArr[_index][indexPath.row];
            
            cell.timeLabel.text = dict[@"event_time"];
            cell.myTitle.text = dict[@"title"];
            [cell.imgView setImageWithURL:[NSURL URLWithString:dict[@"img_url"]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
            
        }
        else
        {
            NSDictionary *dict = _activityArr[_index][indexPath.row];
            
            cell.timeLabel.text = dict[@"event_time"];
            cell.myTitle.text = dict[@"title"];
            [cell.imgView setImageWithURL:[NSURL URLWithString:dict[@"img_url"]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        }
        
        return cell;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_CollectionRow == 2)
    {
        return 200;
    }
    else
    {
        return 261;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (_CollectionRow == 0)
    {
        
        NSDictionary *dict = _giftArr[_index][indexPath.row];
        
        [_mydelegate didSelected:self withUrlStr:dict[@"7zhou_url"]];
        
    }
    else if(_CollectionRow == 1)
    {
        NSDictionary *dict = _activityArr[_index][indexPath.row];
        
        [_mydelegate didSelected:self withUrlStr:dict[@"7zhou_url"]];
    }
    else
    {
        [_mydelegate didSelected:self withIndexPath:indexPath];
    }
}

#pragma mark -SmallCustomCellDelegate

-(void)SmallCustomCell:(LJFSmallCustomCell *)cell buttontag:(NSInteger)tag
{
    NSDictionary *dict = nil;
    
    if (_CollectionRow == 0)
    {
        dict = _giftArr[_index][tag];
    }
    else if (_CollectionRow == 1)
    {
        dict = _activityArr[_index][tag];
    }
    
    NSString *text = dict[@"title"];
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:dict[@"img_url"]]];
    
    UIImage *image = [UIImage imageWithData:data];
    
    [_mydelegate didSelected:self UMSocialWith:text andImage:image];
    
}

@end
